package com.enjoy.xbase.ui.old;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.enjoy.xbase.R;
import com.enjoy.xbase.ui.load.DefaultLoadView;
import com.enjoy.xbase.ui.load.ILoadRefresh;
import com.enjoy.xbase.ui.load.LoadMutual;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

@Deprecated
public abstract  class BaseListFragment extends Fragment {
    private LinearLayout rootView;
    public DefaultLoadView loadView;
    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private RvAdapter adapter;
    private List datas=new ArrayList();
    private int page=0;//从第一页开始

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView==null){
            rootView= (LinearLayout) inflater.inflate(R.layout.xbase_fragment_old_base_list,container,false);
            initView();
            initListener();
            initData();
        }
        return rootView;
    }
    protected void initView(){
        loadView=rootView.findViewById(R.id.v_load);
        refreshLayout = rootView.findViewById(R.id.refresh);
        recyclerView = rootView.findViewById(R.id.list);
        loadView.setLoadBackgroundColor(getBackgroundColor());
        int column=getColumn();
        if (column<=1){
            //创建LinearLayoutManager 对象 这里使用 LinearLayoutManager 是线性布局的意思
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            //设置RecyclerView 布局
            recyclerView.setLayoutManager(linearLayoutManager);
        }else{
            //布局管理器所需参数，上下文
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), column);
            //B 通过布局管理器，可以控制条目排列顺序  true：反向显示  false：正常显示(默认)
            gridLayoutManager.setReverseLayout(false);
            //C 设置RecyclerView显示的方向，是水平还是垂直！！ GridLayoutManager.VERTICAL(默认) false
            gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
            gridLayoutManager.setAutoMeasureEnabled(true);
            recyclerView.setLayoutManager(gridLayoutManager);
        }
        //设置Adapter
        adapter=new RvAdapter();
        recyclerView.setAdapter(adapter);
        refreshLayout.setEnableLoadMore(canLoadmore());
        refreshLayout.setEnableRefresh(canRefresh());
        initTopAndBottomView();
    }
    protected int getBackgroundColor(){
        return Color.parseColor("#ffffff");
    }
    protected void setDataViewBackgroundColor(int color){
        if (recyclerView!=null)recyclerView.setBackgroundColor(color);
    }

    protected void initListener(){
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                requestData(page=0);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                requestData(++page);
            }
        });
        loadView.setLoadMutual(new LoadMutual(){
            @Override
            public View createLoadView(ViewGroup parent) {
                return BaseListFragment.this.createLoadView(parent);
            }

            @Override
            public View createEmptyView(ViewGroup parent) {
                return BaseListFragment.this.createEmptyView(parent);
            }
            @Override
            public View createFaillView(ViewGroup parent) {
                return BaseListFragment.this.createFaillView(parent);
            }

            @Override
            public View createLoadingView(ViewGroup parent) {
                return BaseListFragment.this.createLoadingView(parent);
            }
        });
        loadView.setLoadRefresh(new ILoadRefresh(){
            @Override
            public void onRefresh() {
                loadView.showLoad();
                requestData(page=0);
            }
        });
    }
    protected void initData(){
        loadView.showLoad();
        requestData(page=0);
    }
    private void initTopAndBottomView(){
        View topView=onCreateTopView(rootView);
        if (topView!=null)rootView.addView(topView,0);
        View bottomView=onCreateBottomView(rootView);
        if (bottomView!=null)rootView.addView(bottomView,rootView.getChildCount());
    }
    public boolean isToTop(){
        if (recyclerView!=null){
            return !recyclerView.canScrollVertically(-1);
        }return true;
    }
    public boolean isToBottom(){
        if (recyclerView!=null){
            return !recyclerView.canScrollVertically(1);
        }return true;
    }

    public void refreshData(){
        if (loadView!=null){
            requestData(page=0);
        }
    }
    public void refreshAndLoad(){
        if (loadView!=null){
            loadView.showLoad();
            requestData(page=0);
        }
    }
    public void refreshAndLoading(){
        if (loadView!=null){
            loadView.showLoading();
            requestData(page=0);
        }
    }

    public void setLoadMutual(LoadMutual loadCreateView){
        loadView.setLoadMutual(loadCreateView);
    }

    //创建list顶部视图
    protected View onCreateTopView(ViewGroup container){
        return null;
    }
    //创建list顶部视图
    protected View onCreateBottomView(ViewGroup container){
        return null;
    }
    //列数
    protected int getColumn(){
        return 1;
    }

    //是否支持加载更多
    protected boolean canLoadmore() {
        return false;
    }
    //是否支持刷新
    protected boolean canRefresh() {
        return false;
    }


    public View createLoadView(ViewGroup root) {
        return null;
    }


    public View createFaillView(ViewGroup root) {
        return null;
    }


    public View createEmptyView(ViewGroup root) {
       View view=createRefreshEmptyView(root);
       if (view!=null){
           View emptyView=LayoutInflater.from(getContext()).inflate(R.layout.layout_baselist_empty,root,false);
           emptyView.setBackgroundColor(getBackgroundColor());
           SwipeRefreshLayout swipeRefreshLayout=emptyView.findViewById(R.id.srl_refresh);
           swipeRefreshLayout.setOnRefreshListener(()->{
               swipeRefreshLayout.setRefreshing(false);
               refreshAndLoad();
           });
           swipeRefreshLayout.addView(view);
           return emptyView;
       }else {
           return null;
       }
    }
    protected View createRefreshEmptyView(ViewGroup root) {
        return null;
    }


    public View createLoadingView(ViewGroup root) {
        return null;
    }

    //请求数据
    protected abstract void  requestData(int page);

    //描述：设置请求后的数据
    public void setRequestData(int rpage,List datas){
        loadView.showData();
        if(rpage==0){
            if(datas==null){
                this.datas.clear();
                loadView.showFaill();
            }else{
                if(datas.size()==0){
                    loadView.showEmpty();
                }else{
                    if (canLoadmore()){
                        setEnableLoadmore(true);
                    }
                    loadView.showData();
                }
                this.datas.clear();
                this.datas.addAll(datas);
                this.adapter.notifyDataSetChanged();
            }
            refreshLayout.finishRefresh();
        }else{
            if(datas==null){
                --page;
                loadView.showData();
            }else if(datas.size()==0){
                setEnableLoadmore(false);
                loadView.showData();
            }else{
                this.datas.addAll(datas);
                this.adapter.notifyDataSetChanged();
                loadView.showData();
            }
            refreshLayout.finishLoadMore();
        }
    }

    public <T> T getData(int position){
        return (T)datas.get(position);
    }

    public <T> List<T> getAllData(){
        return datas;
    }

    public void removeData(Object object){
        datas.remove(object);
        if (datas.size()==0){
            loadView.showEmpty();
        }
        adapter.notifyDataSetChanged();
    }


    public void setEnableLoadmore(boolean enable){
        refreshLayout.setEnableLoadMore(enable);
    }

    public void notifyDataSetChanged(){
        adapter.notifyDataSetChanged();
    }

    protected int getRvItemCount() {
        return datas.size();
    }

    protected int getRvItemViewType(int position) {
        return 0;
    }

    protected abstract RecyclerView.ViewHolder onCreateRvViewHolder(ViewGroup parent, int viewType);

    protected abstract void onBindRvViewHolder(RecyclerView.ViewHolder holder, int position);

    class RvAdapter extends RecyclerView.Adapter {

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return onCreateRvViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
           onBindRvViewHolder(holder, position);
        }

        @Override
        public int getItemCount() {
            return getRvItemCount();
        }

        @Override
        public int getItemViewType(int position) {
            return getRvItemViewType(position);
        }
    }

}
